package com.jp.knowledge.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.JsonObject;
import com.jp.knowledge.activity.AttentionListDetailInfo;
import com.jp.knowledge.adapter.attention.AttentionListAdapter;
import com.jp.knowledge.broad.LocalBroadCast;
import com.jp.knowledge.http.HttpHelper;
import com.jp.knowledge.model.AttentionData;
import com.jp.knowledge.model.IModel;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 528787067@qq.com on 2017/3/20.
 */

public class AttentionListOfNameFragment extends AttentionStatisticsFragment {

    private List<AttentionData> attentionDatas;
    private AttentionListAdapter attentionListAdapter;

    private LocalBroadCast localBroadCast;
    private LocalBroadcastManager localBroadcastManager;

    public static AttentionListOfNameFragment newInstance(String id, int type){
        AttentionListOfNameFragment fragment = new AttentionListOfNameFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("id", id);
        bundle.putSerializable("type", type);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        attentionDatas = new ArrayList<>();
        attentionListAdapter = new AttentionListAdapter(mContext, attentionDatas);
        attentionListAdapter.setOnItemCallback(new AttentionListAdapter.OnItemCallback() {
            @Override
            public void onItemClick(int position) {
                startActivity(new Intent(mContext, AttentionListDetailInfo.class)
                        .putExtra("id", id)
                        .putExtra("type", type)
                        .putExtra("title", attentionDatas.get(position).getTitle()));
            }
            @Override
            public void onStickTopClick(int position, boolean isTop) {}
            @Override
            public void onDeleteClick(int position) {}
        });

        localBroadcastManager = LocalBroadcastManager.getInstance(mContext);
        localBroadCast = new LocalBroadCast(new LocalBroadCast.BroadCastCallBack() {
            @Override
            public void done(Context context, Intent intent) {
                if(LocalBroadCast.ADD_ATTENTION.equals(intent.getAction())){
                    if(id == null || !id.equals(intent.getStringExtra("groupId")))
                        return;
                    AttentionData attentionData = (AttentionData) intent.getSerializableExtra("attentionData");
                    if(attentionData == null)
                        return;
                    if(intent.getBooleanExtra("isAttention", false)){
                        attentionDatas.add(0, attentionData);
                    } else{
                        for(int i = 0; i < attentionDatas.size(); i++){
                            if(!attentionDatas.get(i).getId().equals(attentionData.getId()))
                                continue;
                            attentionDatas.remove(i);
                            break;
                        }
                    }
                    attentionListAdapter.updateAdapter(attentionDatas);
                }
            }
        });
        localBroadcastManager.registerReceiver(localBroadCast, new IntentFilter(LocalBroadCast.ADD_ATTENTION));
    }

    @Override
    public void onDestroy() {
        localBroadcastManager.unregisterReceiver(localBroadCast);
        super.onDestroy();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View contentView =  super.onCreateView(inflater, container, savedInstanceState);
        contentRv.setLayoutManager(new LinearLayoutManager(mContext));
        contentRv.setAdapter(attentionListAdapter);
        return contentView;
    }

    @Override
    protected void getData(int page, int model){
        JsonObject param = new JsonObject();
        param.addProperty("id", id);
        param.addProperty("page", page);
        HttpHelper.getInstance(mContext).attentionGroupListOfName(param, model, this);
    }

    @Override
    public void onNext(IModel data, int requestType) {
        if(data.getErrcode() != 0){
            onError(requestType);
        } else if(requestType == REFRESH_CODE || requestType == LOADMORE_CODE){
            if(requestType == REFRESH_CODE)
                attentionDatas.clear();
            List<AttentionData> attentionDataList = data.getList(AttentionData.class);
            if(attentionDataList != null && attentionDataList.size() > 0)
                attentionDatas.addAll(attentionDataList);
            attentionListAdapter.updateAdapter(attentionDatas);
        }
    }
}
